﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DataManager;
using DataManager.BLL;
using System.IO;

namespace FILMEK.GUI
{
    public partial class frmSzamla : Form
    {
        FilmManager manager;
        int alkalmazottId;

        /// <summary>
        /// inicializalas
        /// </summary>
        /// <param name="manager"></param>
        public frmSzamla(FilmManager manager, int alkalmazottId)
        {
            InitializeComponent();
            this.manager = manager;
            this.alkalmazottId = alkalmazottId;
            fillCbVevonev();
            fillCbFilmID();
            filllbSzamlaSzam();
        }

        /// <summary>
        /// a cbVasarlok feltoltese a vasarlok nevivel
        /// </summary>
        public void fillCbVevonev()
        {
            cbVasarlok.DataSource = manager.vasarloNevek().Tables[0];
            cbVasarlok.DisplayMember = "Nev";
            cbVasarlok.ValueMember = "Nev";
        }
        public void filllbSzamlaSzam()
        {
            lblSzamlaszam.Text = (Convert.ToInt32(manager.maxSzamlaSzam().Tables[0].Rows[0][0]) + 1).ToString();
        }
        public void fillCbFilmID()
        {
            DataGridViewComboBoxColumn col = (DataGridViewComboBoxColumn)dgvSzamla.Columns[0];
            col.DataSource = manager.getMovieIDList().Tables[0];
            col.DisplayMember = "FilmId";
            col.ValueMember = "FilmId";
            col.AutoComplete = true;
        }

        private void dgvSzamla_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            
            if (e.ColumnIndex == 0 && e.RowIndex >= 0)
            {
                DataSet d = manager.getMovieData(Convert.ToInt32(dgvSzamla.Rows[e.RowIndex].Cells[0].Value));
                dgvSzamla.Rows[e.RowIndex].Cells[1].Value = d.Tables[0].Rows[0][1];
                dgvSzamla.Rows[e.RowIndex].Cells[2].Value = d.Tables[0].Rows[0][5];
                dgvSzamla.Rows[e.RowIndex].Cells[3].Value = d.Tables[0].Rows[0][4];
            }

            if (e.ColumnIndex == 3 && e.RowIndex >= 0)
            {
                int osszeg = 0;
                for (int i = 0; i <= e.RowIndex; i++)
                {
                    osszeg += Int32.Parse(dgvSzamla.Rows[i].Cells[3].Value.ToString()) * Int32.Parse(dgvSzamla.Rows[i].Cells[2].Value.ToString());
                }
                lblOsszsErtek.Text = osszeg.ToString();
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            manager.beginTransaction();
            int SzamlaSzam = Convert.ToInt32(manager.maxSzamlaSzam().Tables[0].Rows[0][0]) + 1;
            int VasarlasID = Convert.ToInt32(manager.maxVasarlasokID().Tables[0].Rows[0][0]) + 1;
            int VasarloID = Convert.ToInt32(manager.vasarloIdja(cbVasarlok.Text.ToString()).Tables[0].Rows[0][0]);
            DateTime Datum = dtpDatum.Value;
            //int Ertek = Convert.ToInt32(lblOsszsErtek.Text.ToString());
            manager.ujVasarlas(VasarlasID, VasarloID, this.alkalmazottId, Datum);
            manager.ujSzamlaFej(SzamlaSzam, Datum, 0, VasarloID, VasarlasID);

            int n = dgvSzamla.Rows.Count, i = 0;
            bool ok = true;
            foreach (DataGridViewRow row in dgvSzamla.Rows)
            {
                if (i++ != n - 1)
                {
                    DataSet d = manager.getMovieDB(Convert.ToInt32(row.Cells[0].Value.ToString()));
                    int hanyvan = Convert.ToInt32(d.Tables[0].Rows[0][0]);
                    if (hanyvan < Convert.ToInt32(row.Cells[2].Value.ToString()))
                    {
                        MessageBox.Show("Nincs elegendo darab a  " + row.Cells[1].Value.ToString() + " filmbol!");
                        ok = false;
                        manager.rollbackTransaction();
                    }
                    else
                    {
                        if (ok)
                        {
                            int SzamlaSor = Convert.ToInt32(manager.maxSzamlaSor().Tables[0].Rows[0][0])+1;
                            manager.ujSzamlaSor(SzamlaSor, SzamlaSzam, Convert.ToInt32(row.Cells[0].Value.ToString()), Convert.ToInt32(row.Cells[3].Value.ToString()) * Convert.ToInt32(row.Cells[2].Value.ToString()));
                            manager.levonFilm(Convert.ToInt32(row.Cells[0].Value.ToString()), Convert.ToInt32(row.Cells[2].Value.ToString()));
                        }
                    }
                }
            }

            if (ok)
            {
                manager.commitTransaction();
                szamlat_genaral();
                MessageBox.Show("Szamla el lett mentve!");
            }
            else
            {
                MessageBox.Show("Hiba! Probald ujra!");
            }
        }

        private void szamlat_genaral()
        {
            int szamlaSzam = Convert.ToInt32(manager.maxSzamlaSzam().Tables[0].Rows[0][0]);
            String fileName = "szamla_" + szamlaSzam + ".txt";
            TextWriter tw = new StreamWriter(fileName);
            
            tw.WriteLine("                           SZÁMLA");

            tw.WriteLine("\n"); tw.WriteLine("\n"); tw.WriteLine("\n");
            tw.WriteLine("SzámlaSzám: " + szamlaSzam);
            tw.WriteLine("Dátum: " + dtpDatum.Value.ToString());
            tw.WriteLine("Eladó neve: " + manager.getAlkalmazottAllInfoById(this.alkalmazottId).Tables[0].Rows[0][0].ToString());
            tw.WriteLine("Vasarló neve: " + cbVasarlok.Text);
            tw.WriteLine("\n"); tw.WriteLine("\n"); tw.WriteLine("\n");
            
            tw.WriteLine("FilmID   |       Film Címe       |   DarabSzám  |   Ár  ");
            tw.WriteLine("\n");
            int ossz = 0;
            int i = 0; int n = dgvSzamla.Rows.Count;
            foreach (DataGridViewRow row in dgvSzamla.Rows)
            {
                if (i++ != n - 1)
                {
                    tw.Write(String.Format("{0,-12} {1,-32} {2,-6} {3,-6}", row.Cells[0].Value.ToString() , row.Cells[1].Value.ToString(), row.Cells[2].Value.ToString(), row.Cells[3].Value.ToString()));
                    ossz += Convert.ToInt32(row.Cells[2].Value) * Convert.ToInt32(row.Cells[3].Value);
                    tw.WriteLine("\n");
                }
            }
            tw.WriteLine("\n"); tw.WriteLine("\n");
            tw.WriteLine("Összesen: " + ossz + "lej" );
            tw.WriteLine("\n"); tw.WriteLine("\n"); tw.WriteLine("\n");
            tw.WriteLine("Aláírás(Vásárló):  ");
            tw.WriteLine("\n"); tw.WriteLine("\n");
            tw.WriteLine("Aláírás(Eladó):  ");
            tw.Close();

        }

    }
}
